// pages/adjust/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        children: [],
        currentRank: '',
        relationId: '',
        behindPersonId: '',
        ids: [],
        clickedTimes: 0
    },
    foword() {
        if(this.data.currentRank == 1) {
            wx.showToast({
                title: '已经是老大',
                icon: 'error',
                duration: 2000
            })
            return
        }else{
            this.data.ids.forEach((item,idx)=>{
                if(item == this.data.id) {
                    if(idx==1) {
                        this.setData({ behindPersonId: null, currentRank:this.data.currentRank-1 })
                    }else {
                        this.setData({ clickedTimes: this.data.clickedTimes-1, currentRank:this.data.currentRank-1 })
                    }
                    
                }
            })
        }
    },
    back() {
        if(this.data.currentRank == this.data.children.length) {
            wx.showToast({
                title: '已经是老小',
                icon: 'error',
                duration: 2000
            })
            return
        }else{
            this.data.ids.forEach((item,idx)=>{
                if(item == this.data.id) {
                    this.setData({ clickedTimes: this.data.clickedTimes+1, currentRank:this.data.currentRank+1 })
                }
            })
        }
    },
    findBehind() {
        const d = this.data
        let selfIndex = d.ids.indexOf(parseInt(d.id))
        if(d.clickedTimes>0) {
            this.setData({ behindPersonId: d.ids[selfIndex + d.clickedTimes] })
        }else{
            this.setData({ behindPersonId: d.ids[selfIndex + d.clickedTimes - 1] })
        }
    },
    //修改
    save() {
        this.findBehind()
        console.log(this.data.relationId)
        console.log(this.data.behindPersonId)
        let params = {
            relation_id: this.data.relationId,
            behind_person_id: this.data.behindPersonId,
        }
        wx.request({
            url: 'https://pg-family-tree-ranpovkkyd.cn-hangzhou.fcapp.run/ft-person/move-relation-rank', 
            method: 'POST',
            header: {'content-type': 'application/json'},
            data: params,
            success (res) {
                if(res.statusCode == 200) {
                    wx.switchTab({
                        url: '/pages/index/index',
                        success: function() {
                            let page = getCurrentPages().pop()
                            if(page == undefined || page == null) return
                            page.onLoad()
                            page.setData({ popboxData:{ boxStatus: false }})
                        }
                    })
                }
            }
        })
    },
    selectIds(arr) {
        let newArr = []
        if(arr.length>0){
            arr.forEach(item=>{
                newArr.push(item.id)
            })
        }
        console.log(newArr)
        this.setData({ ids:newArr })
        newArr.map((item,idx)=>{
            if(item == this.data.id) {
                this.setData({ currentRank:idx+1 })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let c = JSON.parse(options.children)
        this.setData({ id: options.id, relationId: parseInt(options.relationId), children: c })
        this.selectIds(c)
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})